import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
import '../../pages/xhss/xhssHelper.dart';

import '../../model/xhsszt.dart';

class XHSSZTListPage extends StatefulWidget {
  final int typeId;
  @override
  XHSSZTListPage(this.typeId);
  @override
  XHSSZTListPageState createState() => new XHSSZTListPageState();
}

class XHSSZTListPageState extends State<XHSSZTListPage> {
  List<XHSSZT> RList = <XHSSZT>[];
  bool isEnd = false;
  ScrollController _scrollController = new ScrollController();
  bool firstLoading = true;
  bool ifCanAdd = true;
  int canShow = 0;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    canShow = 1;

    _scrollController.addListener(() {
      if (_scrollController.position.pixels ==
          _scrollController.position.maxScrollExtent) {
        getMore();
      }
    });

    getNewer();
  }

  Future<Null> delete(XHSSZT r1) async {
    // await addOne();

    await XHSSZT.Delete(r1);
    setState(() {
      RList.remove(r1);
    });
  }

  onDeleted(XHSSZT r1) => {
        setState(() {
          RList.remove(r1);
        })
      };

  Future<Null> getMore() async {
    // await addOne();
    int count = 5;
    if (isEnd == true) return;
    int page = RList.length ~/ count;
    var list1 = await XHSSZT.GetList(
        "TypeId=@S", {"S": widget.typeId}, page + 1, count);
    var list2 =
        list1.where((p) => RList.any((x) => x.Id == p.Id) == false).toList();

    setState(() {
      if (list1.length < count) isEnd = true;
      RList.insertAll(RList.length, list2);
    });
  }

  Future<Null> getRefrush() async {
    var list1 = await XHSSZT.GetList("TypeId=@S", {"S": widget.typeId}, 1, 5);

    setState(() {
      RList = list1;
    });
  }

  Future<Null> getNewer() async {
    var list1 = await XHSSZT.GetList("TypeId=@S", {"S": widget.typeId}, 1, 10);
    setState(() {
      firstLoading = false;
    });

    if (list1.length == 0) return;

    var list2 =
        list1.where((p) => RList.any((x) => x.Id == p.Id) == false).toList();
    if (list2.length == 0) return;

    list2.insertAll(list2.length, RList);

    setState(() {
      firstLoading = false;
      RList = list2;
    });
  }

  @override
  Widget build(BuildContext context) {
    if (RList == null) return new Container();
    // TODO: implement build
    return new Scaffold(
        floatingActionButton: FloatingActionButton(
            child: Icon(Icons.refresh), onPressed: getRefrush),
        body: firstLoading == true
            ? Center(
                child: SpinKitCircle(
                color: Colors.blue,
              ))
            : Container(
                margin: EdgeInsets.only(top: 8.0),
                child: new RefreshIndicator(
                  child: ListView.builder(
                    itemCount: RList.length + 1,
                    controller: _scrollController,
                    physics: const AlwaysScrollableScrollPhysics(),
                    itemBuilder: (BuildContext context, int index) {
                      if (index != RList.length) {
                        return XHSSHelper.getXHSSZTCard(context, RList[index]);
                      } else {
                        if (isEnd == true) {
                          return Container(
                              margin: EdgeInsets.all(12.0),
                              child: Center(
                                  child: Text(
                                "没有更多了",
                                style: TextStyle(color: Colors.grey),
                              )));
                        } else {
                          return new Container();
                        }
                      }
                    },
                  ),
                  onRefresh: getNewer,
                )));
  }
}
